package com.metaera.app.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.lxj.xpopup.XPopup;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.SpecialDetailsPresenter;
import com.metaera.app.data.model.SpecialDetailsResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.HomeNewsAdapter;
import com.metaera.app.ui.core.BaseRequestActivity;
import com.metaera.app.ui.popup.SharePopup;
import com.metaera.app.utils.ViewUtils;
import com.metaera.app.utils.image.GlideApp;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 精选专题详情
 */
public class ChoiceTopicDetailsActivity extends BaseRequestActivity {

    public static Intent newInstance(Context context, int id) {
        Intent intent = new Intent(context, ChoiceTopicDetailsActivity.class);
        intent.putExtra("ID", id);
        return intent;
    }

    @Override
    protected void initStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        StatusBarUtil.setLightMode(this);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_choice_topic_details;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        TextView tvDes = findViewById(R.id.tv_des);
        TextView tvTitle = findViewById(R.id.tv_title);
        ImageView ivCover = findViewById(R.id.iv_cover);
        ViewGroup layoutParent = findViewById(R.id.layout_parent);
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_container));

        int id = getIntent().getIntExtra("ID", 0);

        SpecialDetailsPresenter presenter = new SpecialDetailsPresenter(this);
        presenter.setModelAndView(data -> {
            layoutParent.removeAllViews();
            
            // 标题
            tvDes.setText(data.getDesc());
            tvTitle.setText(data.getName());
            GlideApp.with(this).load(data.getPic()).normal().into(ivCover);

            findViewById(R.id.iv_share).setOnClickListener(v -> {
                new XPopup.Builder(v.getContext())
                        .asCustom(
                                new SharePopup(
                                        v.getContext(),
                                        data.getName(),
                                        data.getDesc(),
                                        ApiParam.LINK_SHARE_8 + data.getId(),
                                        R.mipmap.ic_launcher
                                )
                        )
                        .show();
            });

            List<SpecialDetailsResponse.Subordinate> subordinate = data.getSubordinate();
            for (SpecialDetailsResponse.Subordinate sub : subordinate) {
                renderList(sub, layoutParent);
            }

        });

        presenter.execute(id);

        // listener
        findViewById(R.id.iv_arrow_back).setOnClickListener(v -> finish());

    }

    private void renderList(SpecialDetailsResponse.Subordinate sub, ViewGroup layoutParent) {
        View inflate = LayoutInflater.from(layoutParent.getContext())
                .inflate(R.layout.include_choice_topic_recycler, layoutParent, false);
        TextView tvTitle = inflate.findViewById(R.id.tv_title);
        RecyclerView recyclerView = inflate.findViewById(R.id.recycler_view);
        HomeNewsAdapter adapter = new HomeNewsAdapter();
        adapter.setList(sub.getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(true);
        tvTitle.setText(sub.getName());
        layoutParent.addView(inflate);
    }

}
